/**
 * @author LiuYan
 * @date 2023/5/16
 */
public class InstanceofOptimization {

    public static void main(String[] args) {

        // Java8 instanceof
        final String oldStr8;
        Object oldObj = "java8";
        if (oldObj instanceof String) {
            String str = (String) oldObj;
            oldStr8 = str.toUpperCase();
        } else {
            oldStr8 = "";
        }
        System.out.println("Java8 instanceof:" + oldStr8);

        // Java14 instanceof 运算符检查 obj 是否是 String 类型的实例，如果是，则使用强制类型转换将 obj 转换为 String 对象，并在之后的代码中使用该对象
        final String newStr14;
        Object newObj = "java14";
        if (newObj instanceof String s) {
            newStr14 = s.toUpperCase();
        } else {
            newStr14 = "";
        }
        System.out.println("Java14 instanceof:" + newStr14);

    }
}
